<?php

namespace app\api\command;

use app\common\model\Packages;
use app\common\model\User;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class Orderdailimoretwo extends Command
{
    protected function configure()
    {
        $this->setName('order_daili_more_two')->setHelp('用户端平台2、3、4多次结算')->setDescription('用户端平台2、3、4多次结算');

    }

    protected function execute(Input $input, Output $output)
    {
        $orderModel            = new \app\common\model\Order();
        $userModel              = new User();
        $where['is_more']      = 2;
        $where['is_agency']    = 2;
        $where['order_status'] = 5;
        $where['more_num']     = 1;
        $where['import_num']   = 0;
        $where['audit_status'] = 2;
        $where['platform_type'] = ['neq',1];
        $order                 = $orderModel->where($where)->order('id desc')->select();
//        $order                 = $orderModel->where($where)->order('id desc')->select();
//        dump($order);exit();
        if ($order) {
            foreach ($order as $k => $v) {
                sleep(2);
                $v->more_num += 1;
                $packageInfo = Packages::where(['id' => $v['goods_id']])->find();
                $huafei            = $packageInfo['ciyue_set_meal'];
                if ($huafei) {
                    $userModel::money($huafei, $v['user_id'], '话费报销', $v['id'],
                        $packageInfo['id']);
                }
                jiangli($v, $packageInfo, 1, $v->more_num);
                if ($v['parent_id']) {
                    jiangli($v, $packageInfo, 2, $v->more_num);
                }
                if ($v['two_id']) {
                    jiangli($v, $packageInfo, 3, $v->more_num);
                }
                //区县
                if ($v['agency_area_id']) {
                    jiangli($v, $packageInfo, 4, $v->more_num);
                }
                //小区
                if ($v['plot_id']) {
                    jiangli($v, $packageInfo, 5, $v->more_num);
                }
                $v->save();
            }

        }
    }


//    protected function execute(Input $input, Output $output)
//    {
//        $orderModel            = new \app\common\model\Order();
//        $userModel             = new User();
//        $where['is_more']      = 2;
//        $where['is_agency']    = 2;
//        $where['order_status'] = 5;
//        $where['more_num']     = ['gt', 0];
//        $where['import_num']   = 0;
//        $order                 = $orderModel->where($where)->where('two_js_time','<',time())->order('id desc')->select();
//        if ($order) {
//            foreach ($order as $k => $v) {
//                sleep(2);
//                $packageInfo = Packages::where(['id' => $v['goods_id']])->find();
//                if (!empty($packageInfo['duoci'])) {
//                    $duociConfig = json_decode($packageInfo['duoci'], true);
//                    $userInfo    = $userModel->where(['id' => $v['user_id']])->find();
//                    $key         = $v['more_num'];
//                    if ($userInfo) {
//                        if (!empty($duociConfig[$key]['zigou'])) {
//                            $userModel::money($duociConfig[$key]['zigou'],$userInfo['user_id'], '自购奖励', $v['id'],
//                                $packageInfo['id']);
//                        }
//                        if (!empty($duociConfig[$key]['one']) && $userInfo['parent_id']) {
//                            $userModel::money($duociConfig[$key]['one'],$userInfo['parent_id'], '直推奖励', $v['id'],
//                                $packageInfo['id']);
//                        }
//                        if (!empty($duociConfig[$key]['two']) && $userInfo['two_id']) {
//                            $userModel::money($duociConfig[$key]['two'], $userInfo['two_id'], '下下级贡献', $v['id'],
//                                $packageInfo['id']);
//                        }
//                    }
//                    $cityAgency = db('agency')->where(['area' => $v['shipping_area'], 'type' => 2])->find();
//                    if ($cityAgency && !empty($duociConfig[$key]['area'])) {
//                        if ($cityAgency->money >= config('site.hou_county_money') + config('site.county_money')) {
//                            $userModel::money($duociConfig[$key]['area'], $cityAgency['user_id'], '区县运营分润', $v['id'],
//                                $packageInfo['id']);
//                        } else {
//                            db('agency')->where(['id' => $cityAgency->id])->setInc('money', $duociConfig[$key]['area']);
//                        }
//                    }
//                    $plot_id = 0;
//                    if ($userInfo['plot_id']) {
//                        $plot_id = $userInfo['plot_id'];
//                    } else {
//                        $profitAgency = db('agency')->where(['area' => $v['shipping_address'], 'type' => 1])->find();
//                        if ($profitAgency) {
//                            $plot_id = $profitAgency['user_id'];
//                        }
//                    }
//                    if ($plot_id && !empty($duociConfig[$key]['xiaoqu'])) {
//                        if ($profitAgency->money > config('site.plot_money') + config('site.hou_plot_money')) {
//                            $userModel::money($duociConfig[$key]['xiaoqu'], $plot_id, '小区运营分润', $v['id'],
//                                $packageInfo['id']);
//                        } else {
//                            db('agency')->where(['id' => $profitAgency->id])->setInc('money',
//                                $duociConfig[$key]['xiaoqu']);
//                        }
//                    }
//                    $v->more_num += 1;
//                    $r           = $v->save();
//                    if ($r) {
//                        echo "操作成功";
//                    } else {
//                        echo "更新失败";
//                    }
//                }
//            }
//
//        }
//    }
}
